package com.mis.service.impl.data;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import com.jeecg.dao.BaseDaoI;
import com.jeecg.dao.jdbc.JdbcDao;
import com.jeecg.model.Tuser;
import com.jeecg.pageModel.DataGrid;
import com.jeecg.service.impl.BaseServiceImpl;
import com.jeecg.util.dbcommon.SearchSqlGenerateUtil;
import com.jeecg.util.excel.ExcelExportUtil;
import com.jeecg.util.pojo.MyBeanUtils;
import com.mis.entity.data.EmployeeEntity;
import com.mis.page.data.EmployeePage;
import com.mis.service.data.EmployeeServiceI;

/**   
 * @Title: ServiceImpl
 * @Description: 单表模型Test
 * @author zhangdaihao
 * @date 2011-12-31 14:18:16
 * @version V1.0   
 *
 */
@Service("employeeService")
public class EmployeeServiceImpl extends BaseServiceImpl implements EmployeeServiceI {

	@Autowired
	//SQL 使用JdbcDao
	private JdbcDao jdbcDao;
	private BaseDaoI<EmployeeEntity> employeeEntityDao;

	public BaseDaoI<EmployeeEntity> getEmployeeEntityDao() {
		return employeeEntityDao;
	}
	@Autowired
	public void setEmployeeEntityDao(BaseDaoI<EmployeeEntity> employeeEntityDao) {
		this.employeeEntityDao = employeeEntityDao;
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public DataGrid datagrid(EmployeePage employeePage) {
		DataGrid j = new DataGrid();
		j.setRows(getPagesFromEntitys(find(employeePage)));
		j.setTotal(total(employeePage));
		return j;
	}

	private List<EmployeePage> getPagesFromEntitys(List<EmployeeEntity> employeeEntitys) {
		List<EmployeePage> employeePages = new ArrayList<EmployeePage>();
		if (employeeEntitys != null && employeeEntitys.size() > 0) {
			for (EmployeeEntity tb : employeeEntitys) {
				EmployeePage b = new EmployeePage();
				BeanUtils.copyProperties(tb, b);
				employeePages.add(b);
			}
		}
		return employeePages;
	}

	private List<EmployeeEntity> find(EmployeePage employeePage) {
		String hql = "from EmployeeEntity t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(employeePage, hql, values);

		if (employeePage.getSort() != null && employeePage.getOrder() != null) {
			hql += " order by " + employeePage.getSort() + " " + employeePage.getOrder();
		}
		return employeeEntityDao.find(hql, employeePage.getPage(), employeePage.getRows(), values);
	}

	private Long total(EmployeePage employeePage) {
		String hql = "select count(*) from EmployeeEntity t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(employeePage, hql, values);
		return employeeEntityDao.count(hql, values);
	}

	private String addWhere(EmployeePage employeePage, String hql, List<Object> values) {
	    //循环查询条件Page的所有[Integer][String]类型的字段，如果字段不为空则动态加上查询条件
		//-----------------------------------------------------
		StringBuffer hqlbf = new StringBuffer(hql);
		
		EmployeeEntity employeeEntity = new EmployeeEntity();
		BeanUtils.copyProperties(employeePage, employeeEntity);
		SearchSqlGenerateUtil.createSearchParamsHql(hqlbf, values, employeeEntity);
		hql = hqlbf.toString();
		//-----------------------------------------------------
		if (employeePage.getCcreatedatetimeStart() != null) {
			hql += " and createDt>=? ";
			values.add(employeePage.getCcreatedatetimeStart());
		}
		if (employeePage.getCcreatedatetimeEnd() != null) {
			hql += " and createDt<=? ";
			values.add(employeePage.getCcreatedatetimeEnd());
		}
		if (employeePage.getCmodifydatetimeStart() != null) {
			hql += " and modifyDt>=? ";
			values.add(employeePage.getCmodifydatetimeStart());
		}
		if (employeePage.getCmodifydatetimeEnd() != null) {
			hql += " and modifyDt<=? ";
			values.add(employeePage.getCmodifydatetimeEnd());
		}
		return hql;
	}

	public void add(EmployeePage employeePage) {
		if (employeePage.getObid() == null || employeePage.getObid().trim().equals("")) {
			employeePage.setObid(UUID.randomUUID().toString());
		}
		EmployeeEntity t = new EmployeeEntity();
		BeanUtils.copyProperties(employeePage, t);
		employeeEntityDao.save(t);
	}

	public void update(EmployeePage employeePage) throws Exception {
		EmployeeEntity t = employeeEntityDao.get(EmployeeEntity.class, employeePage.getObid());
	    if(t != null) {
			MyBeanUtils.copyBeanNotNull2Bean(employeePage, t);
		}
	}

	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				EmployeeEntity t = employeeEntityDao.get(EmployeeEntity.class, id);
				if (t != null) {
					employeeEntityDao.delete(t);
				}
			}
		}
	}
	
	//----------------------------------------------------------------
	//update-begin--Author:shiyanping  Date:20130315 for：增加excel导出
	//update-end--Author:shiyanping  Date:20130315 for：增加excel导出
	//----------------------------------------------------------------
	@Override
	public InputStream exportXls(EmployeePage employeePage)  throws  Exception{
		//获取数据
		List<EmployeeEntity> listEmployee = listAll(employeePage);
		File file = new File(new Date(0).getTime()+".xls");
		OutputStream outputStream = new FileOutputStream(file);
		ExcelExportUtil.exportExcel("用户信息", EmployeeEntity.class, listEmployee, outputStream);
		InputStream is = new BufferedInputStream(new FileInputStream(file.getPath()));
		file.delete();		
		return is;
	}	

	public EmployeeEntity get(EmployeePage employeePage) {
		return employeeEntityDao.get(EmployeeEntity.class, employeePage.getObid());
	}

	public EmployeeEntity get(String obid) {
		return employeeEntityDao.get(EmployeeEntity.class, obid);
	}
	public List<EmployeeEntity> listAll(EmployeePage employeePage) {
		String hql = "from EmployeeEntity where 1 = 1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(employeePage, hql, values);
		List<EmployeeEntity> list = employeeEntityDao.find(hql,values);
		return list;
	}
}
